-- NUEVOS IMPORTES RG.830-RETENCION DE GANANCIAS VIGENTES DESDE 01/08/2019.-- Utilizamos una tabla que simplemente tiene una booleana que indica si debemos migrar o no.
-- Esto es para que no se ejecute este query si esta version llega despues que abaco o el cliente haya hecho actualizaciones en padrig.


CREATE TABLE cliente.debo_migrar
(sino character varying) WITH (OIDS=TRUE);
ALTER TABLE cliente.debo_migrar OWNER TO abacouser;

INSERT INTO cliente.debo_migrar(sino)
SELECT CASE WHEN coalesce((select max(x.retencionmin) from padrig x where x.cod = 'Caso1Insc'), 0.00) <= 150.00 THEN 'S' 
       ELSE 'N' END;

-- Proceso comun de migracion.

update padrig set retencionmin = 240 where ((select sino from cliente.debo_migrar) = 'S');
update padrig set retencionmin = 1020 where cod = 'Caso1NoInsc' and ((select sino from cliente.debo_migrar) = 'S');
update padrig set impomin = 11200 where cod in ('Caso1Insc', 'Caso1NoInsc') and ((select sino from cliente.debo_migrar) = 'S');
update padrig set impomin = 224000 where cod in ('Caso2Insc', 'Caso2NoInsc') and ((select sino from cliente.debo_migrar) = 'S');
update padrig set impomin = 67170 where cod in ('Caso3Insc', 'Caso3NoInsc') and ((select sino from cliente.debo_migrar) = 'S');
update padrig set impomin = 67170 where cod in ('Caso4Insc', 'Caso4NoInsc') and ((select sino from cliente.debo_migrar) = 'S');
update padrig set impomin = 16830 where cod in ('Caso5Insc', 'Caso5NoInsc') and ((select sino from cliente.debo_migrar) = 'S');
update padrig set impomin = 67170 where cod in ('Caso6Insc', 'Caso6NoInsc') and ((select sino from cliente.debo_migrar) = 'S');

update padrig
       set nombre = case when cod = 'Caso4Insc' and tasa = 9 then 'Insc-Hon. a Dtor. S.A., sindico(8000)'
                         when cod = 'Caso4Insc' and tasa = 12 then 'Insc-Hon. a Dtor. S.A., sindico(16000)'
                         when cod = 'Caso4Insc' and tasa = 15 then 'Insc-Hon. a Dtor. S.A., sindico(24000)'
                         when cod = 'Caso4Insc' and tasa = 19 then 'Insc-Hon. a Dtor. S.A., sindico(32000)'
                         when cod = 'Caso4Insc' and tasa = 23 then 'Insc-Hon. a Dtor. S.A., sindico(48000)'
                         when cod = 'Caso4Insc' and tasa = 27 then 'Insc-Hon. a Dtor. S.A., sindico(64000)'
                         when cod = 'Caso4Insc' and tasa = 31 then 'Insc-Hon. a Dtor. S.A., sindico(96000)'
			 when cod = 'Caso5Insc' and tasa = 9 then 'Insc-Pago Prof. liberales(8000)'
                         when cod = 'Caso5Insc' and tasa = 12 then 'Insc-Pago Prof. liberales(16000)'
			 when cod = 'Caso5Insc' and tasa = 15 then 'Insc-Pago Prof. liberales(24000)'
			 when cod = 'Caso5Insc' and tasa = 19 then 'Insc-Pago Prof. liberales(32000)'
			 when cod = 'Caso5Insc' and tasa = 23 then 'Insc-Pago Prof. liberales(48000)'
			 when cod = 'Caso5Insc' and tasa = 27 then 'Insc-Pago Prof. liberales(64000)'
			 when cod = 'Caso5Insc' and tasa = 31 then 'Insc-Pago Prof. liberales(96000)'			
                         else nombre
                    end,
           desde = case when cod = 'Caso4Insc' and tasa = 9 then 8000
                        when cod = 'Caso4Insc' and tasa = 12 then 16000
                        when cod = 'Caso4Insc' and tasa = 15 then 24000
			when cod = 'Caso4Insc' and tasa = 19 then 32000
			when cod = 'Caso4Insc' and tasa = 23 then 48000
			when cod = 'Caso4Insc' and tasa = 27 then 64000
			when cod = 'Caso4Insc' and tasa = 31 then 96000
			when cod = 'Caso5Insc' and tasa = 9 then 8000
                        when cod = 'Caso5Insc' and tasa = 12 then 16000
                        when cod = 'Caso5Insc' and tasa = 15 then 24000
			when cod = 'Caso5Insc' and tasa = 19 then 32000
			when cod = 'Caso5Insc' and tasa = 23 then 48000
			when cod = 'Caso5Insc' and tasa = 27 then 64000
			when cod = 'Caso5Insc' and tasa = 31 then 96000
                        else desde
                    end,
           hasta = case when cod = 'Caso4Insc' and tasa = 5 then 8000
			when cod = 'Caso4Insc' and tasa = 9 then 16000
			when cod = 'Caso4Insc' and tasa = 12 then 24000
			when cod = 'Caso4Insc' and tasa = 15 then 32000
			when cod = 'Caso4Insc' and tasa = 19 then 48000
			when cod = 'Caso4Insc' and tasa = 23 then 64000
			when cod = 'Caso4Insc' and tasa = 27 then 96000
                        when cod = 'Caso5Insc' and tasa = 5 then 8000
			when cod = 'Caso5Insc' and tasa = 9 then 16000
			when cod = 'Caso5Insc' and tasa = 12 then 24000
			when cod = 'Caso5Insc' and tasa = 15 then 32000
			when cod = 'Caso5Insc' and tasa = 19 then 48000
			when cod = 'Caso5Insc' and tasa = 23 then 64000
			when cod = 'Caso5Insc' and tasa = 27 then 96000
                        else hasta
                    end,
           impofijo = case when cod = 'Caso4Insc' and tasa = 9 then 400
                           when cod = 'Caso4Insc' and tasa = 12 then 1120
                           when cod = 'Caso4Insc' and tasa = 15 then 2080
			   when cod = 'Caso4Insc' and tasa = 19 then 3280
			   when cod = 'Caso4Insc' and tasa = 23 then 6320
			   when cod = 'Caso4Insc' and tasa = 27 then 10000
			   when cod = 'Caso4Insc' and tasa = 31 then 18640
			   when cod = 'Caso5Insc' and tasa = 9 then 400
                           when cod = 'Caso5Insc' and tasa = 12 then 1120
                           when cod = 'Caso5Insc' and tasa = 15 then 2080
			   when cod = 'Caso5Insc' and tasa = 19 then 3280
			   when cod = 'Caso5Insc' and tasa = 23 then 6320
			   when cod = 'Caso5Insc' and tasa = 27 then 10000
			   when cod = 'Caso5Insc' and tasa = 31 then 18640
                           else impofijo
                    end
where ((select sino from cliente.debo_migrar) = 'S');



-- =====================================
SELECT aaversion('409.04');
-- =====================================

